﻿using OSSUtility.Device;
using Ozone.Help;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Ozone.Device
{



    public class DevVisualFailure : DevBase
    {
        //构造函数
        public DevVisualFailure(int CommType, string FSrvIP, int FPort) : base(CommType, FSrvIP, FPort)
        {
        }






        //调用拍照方法
        //获取到图像路径

        //发送参数 获取 离心管状态
        //图片不做处理还在原位置


        //IP 192.168.0.100
        //端口    7930 
        //发送图片地址
        //d:\Picture\2025-28-16-01-54_CapturePircture.jpg;1/r/n

        //返回字符串 
        //1,1,0,1,0,1,0,1

        //八个字符串 

        //1 存在离心管

        //0  不存在离心管

        //第一位 是正右方向 试管   逆时针旋转







        /// <summary>
        /// 拍照
        /// </summary>
        /// <returns></returns>
        public string CapturePictures()
        {
            try
            {
                LogHelper.Debug("调用摄像头拍照");
                //string fileAddress = VideoParam.Instance.hKSdkMethods3.CapturePictures(ConfigurationManager.AppSettings["CaptureAddress"].ToString(), VideoParam.Instance.m_userID3, out VideoParam.Instance.ErrorMessage);
                string fileAddress = "";
                LogHelper.Debug("获取到拍照图像地址：" + fileAddress);
                string path = ExtractPurePath(fileAddress);


                // 或者使用更规范的Path类方法
                path = Path.GetFullPath(path.Replace("//", "\\"));
                LogHelper.Debug("解析后图像完整地址：" + path);

                if (File.Exists(path))
                {
                    LogHelper.Debug("图像路径验证存在");
                    return path;
                }
                else
                {
                    LogHelper.Debug("图像路径验证不存在");
                    return "";
                }
            }
            catch (Exception)
            {

                throw;
            }
        }

        private static string ExtractPurePath(string rawString)
        {
            // 处理包含前缀描述的路径字符串
            int startIndex = rawString.IndexOf("D://");
            if (startIndex >= 0)
            {
                return rawString.Substring(startIndex).Trim();
            }
            return rawString;
        }




        public string OCRPhoto(string fileAddress)
        {


            try
            {
                string command = fileAddress + ";1\r\n";
                LogHelper.Debug("进行OCR图像识别:" + command);

                string recvStr = session_.sendAndReadStr(command, 1000);
                LogHelper.Debug("OCR图像识别返回值:" + recvStr);

                return recvStr;
            }
            catch (Exception)
            {
                return string.Empty;
            }


        }


        public string OCRPhotoResult()
        {
            string OCRResult = string.Empty;
            try
            {
                string fileAddress = CapturePictures();
                if (!string.IsNullOrEmpty(fileAddress))
                {

                    OCRResult = OCRPhoto(fileAddress);
                    if (string.IsNullOrEmpty(OCRResult))
                    {
                        OCRResult = OCRPhoto(fileAddress);
                    }
                    if (string.IsNullOrEmpty(OCRResult))
                    {
                        OCRResult = OCRPhoto(fileAddress);
                    }
                }


            }
            catch (Exception)
            {

                OCRResult = string.Empty;
            }
            return OCRResult;

        }






    }
}
